package org.ellisande.grid.tags;

import java.util.EnumSet;

import javax.servlet.jsp.JspWriter;

import org.ellisande.grid.tags.support.TagBuilder;
import org.ellisande.tld.annotations.Attribute;
import org.ellisande.tld.annotations.Description;
import org.ellisande.tld.annotations.Info;
import org.ellisande.tld.annotations.Tag;

@Tag
@Info("Tag for creating a nav bar at the top or bottom of the page. The nav bar can be static or fixed.")
public class NavBarTag extends AbstractTag {

	private static final long serialVersionUID = -787802031041818956L;

	private static enum Type {
		STATIC, FIXED;

		private Type() {
		}

		public String toString() {
			return name();
		}
	}

	private static enum Location {
		TOP("top"), BOTTOM("bottom");

		private String type;

		private Location(String type) {
			this.type = type;
		}

		public String toString() {
			return type;
		}
	}

	@Attribute
	@Description("Defines the type of nav bar. Allowed values: static | fixed. Default: static.")
	private Type type = Type.STATIC;

	@Attribute
	@Description("Defines the position of the nav bar. Required for type fixed. Allowed values: top | bottom.")
	private Location locale = Location.TOP;

	private static final String FIXED_NAV_BASE = "navbar-fixed-";

	public void setLocale(String loc) {
		for (Location locale : EnumSet.allOf(Location.class)) {
			if (locale.name().equalsIgnoreCase(loc)) {
				this.locale = locale;
				return;
			}
		}
	}

	public void setType(String type) {
		for (Type current : EnumSet.allOf(Type.class)) {
			if (current.name().equalsIgnoreCase(type)) {
				this.type = current;
				return;
			}
		}
	}

	@Override
	public void startTag() throws Exception {
		TagBuilder nav = createTagBuilder("div");
		TagBuilder navInner = createTagBuilder("div");
		TagBuilder container = createTagBuilder("div");

		nav.addStyle("navbar");
		if (Type.FIXED.equals(type)) {
			String fixedClass = FIXED_NAV_BASE + locale.toString();
			nav.addStyle(fixedClass);
		}

		navInner.addStyle("navbar-inner");

		container.addStyle("container-fluid");

		JspWriter out = pageContext.getOut();
		out.write(nav.compileStartTag());
		out.write(navInner.compileStartTag());
		out.write(container.compileStartTag());

	}

	@Override
	public void endTag() throws Exception {
		JspWriter out = pageContext.getOut();
		out.write("</div>");
		out.write("</div>");
		out.write("</div>");

	}

}
